home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 3 / adb / g-hesora < prev    next >
Text File  |  1996-02-12  |  6KB  |  134 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                     G N A T . H E A P _ S O R T _ A                      --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --     Copyright (C) 1992,1993,1994,1995 Free Software Foundation, Inc.     --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. package body GNAT.Heap_Sort_A is
  37.  
  38.    ----------
  39.    -- Sort --
  40.    ----------
  41.  
  42.    --  We are using the classical heapsort algorithm (i.e. Floyd's Treesort3)
  43.    --  as described by Knuth (ref???) with the modification that is mentioned
  44.    --  in excercise ???. For more details on this algorithm, see Robert B. K.
  45.    --  Dewar PhD thesis "The use of Computers in the X-ray Phase Problem".
  46.    --  University of Chicago, 1968.
  47.  
  48.    procedure Sort (N : Positive; Move : Move_Procedure; Lt : Lt_Function) is
  49.  
  50.       Max : Positive := N;
  51.       --  Current Max index in tree being sifted
  52.  
  53.       procedure Sift (S : Positive);
  54.       --  This procedure sifts up node S, i.e. converts the subtree rooted
  55.       --  at node S into a heap, given the precondition that any sons of
  56.       --  S are already heaps. On entry, the contents of node S is found
  57.       --  in the temporary (index 0), the actual contents of node S on
  58.       --  entry are irrelevant. This is just a minor optimization to avoid
  59.       --  what would otherwise be two junk moves in phase two of the sort.
  60.  
  61.       procedure Sift (S : Positive) is
  62.          C      : Positive := S;
  63.          Son    : Positive;
  64.          Father : Positive;
  65.  
  66.       begin
  67.          --  This is where the optimization is done, normally we would do a
  68.          --  comparison at each stage between the current node and the larger
  69.          --  of the two sons, and continue the sift only if the current node
  70.          --  was less than this maximum. In this modified optimized version,
  71.          --  we assume that the current node will be less than the larger
  72.          --  son, and unconditionally sift up. Then when we get to the bottom
  73.          --  of the tree, we check parents to make sure that we did not make
  74.          --  a mistake. This roughly cuts the number of comparisions in half,
  75.          --  since it is almost always the case that our assumption is correct.
  76.  
  77.          --  Loop to pull up larger sons
  78.  
  79.          loop
  80.             Son := 2 * C;
  81.             exit when Son > Max;
  82.  
  83.             if Son < Max and then Lt (Son, Son + 1) then
  84.                Son := Son + 1;
  85.             end if;
  86.  
  87.             Move (Son, C);
  88.             C := Son;
  89.          end loop;
  90.  
  91.          --  Loop to check fathers
  92.  
  93.          while C /= S loop
  94.             Father := C / 2;
  95.  
  96.             if Lt (Father, 0) then
  97.                Move (Father, C);
  98.                C := Father;
  99.             else
  100.                exit;
  101.             end if;
  102.          end loop;
  103.  
  104.          --  Last step is to pop the sifted node into place
  105.  
  106.          Move (0, C);
  107.       end Sift;
  108.  
  109.    --  Start of processing for Sort
  110.  
  111.    begin
  112.       --  Phase one of heapsort is to build the heap. This is done by
  113.       --  sifting nodes N/2 .. 1 in sequence.
  114.  
  115.       for J in reverse 1 .. N / 2 loop
  116.          Move (J, 0);
  117.          Sift (J);
  118.       end loop;
  119.  
  120.       --  In phase 2, the largest node is moved to end, reducing the size
  121.       --  of the tree by one, and the displaced node is sifted down from
  122.       --  the top, so that the largest node is again at the top.
  123.  
  124.       while Max > 1 loop
  125.          Move (Max, 0);
  126.          Move (1, Max);
  127.          Max := Max - 1;
  128.          Sift (1);
  129.       end loop;
  130.  
  131.    end Sort;
  132.  
  133. end GNAT.Heap_Sort_A;
  134.